<?php

namespace __TARGET_BUNDLE__\Controller\Admin;

use __TARGET_BUNDLE__\Setting\__TARGET_ENTITY__Setting;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use __TARGET_BUNDLE__\Base\BaseController4Admin;
use __TARGET_BUNDLE__\Entity\__TARGET_ENTITY__ as Entity;

/**
 * @route("/admin/")
 */
class __TARGET_ENTITY__Controller extends BaseController4Admin
{

    /**
     * @route("__TARGET_ENTITY__/add",name="__TARGET_ENTITY__PreAdd")
     */
    public function add()
    {
        $uid = $this->needToBeAdmin();
        //通用代码
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        //return $this->comAdd();
        $entity = "";
        if (!$entity) {$entity = new $this->entitySetting->entityPath();}
        $form = $this->createMyForm(
            $this->entitySetting->SettingPath,
            $entity,
            $this->generateUrl("__TARGET_ENTITY__DoAdd")
        );
        $show = $form->createView();
        $data = array(
            "title" => $this->entitySetting->entityCnName,
            "submitUrl" => $this->generateUrl("__TARGET_ENTITY__DoAdd"),
            "submitType" => "Post",
            "show"   => $show,
        );
        return $this->render("App/__TARGET_ENTITY__/edit.php.twig",$data);
    }

    /**
     * @route("__TARGET_ENTITY__/add/do",name="__TARGET_ENTITY__DoAdd")
     *
     *
     */
    public function doAdd(Request $request)
    {
        $uid = $this->needToBeAdmin();
        //私有代码
        $entity = new Entity();
        //通用代码
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        //return $this->comDoAdd($request,$entity);
        if (!$entity) {
            $entity = new $this->entitySetting->entityPath();
        }
        $form = $this->createMyForm($this->entitySetting->SettingPath,  $entity);
        $form->handleRequest($request);
        if ($form->isSubmitted()) {
            $em = $this->getDoctrine()->getManager();
            /**@var $entity Entity*/
            $em->persist($entity);
            $em->flush();
            $urlData = [];//传参
            return $this->js_out("保存成功",$this->generateUrl($this->entitySetting->entityName."Lists",$urlData));
        } else {
            return $this->js_out("系统错误，请联系管理员",$this->generateUrl($this->entitySetting->entityName."PreAdd"));
        }
    }

    /**
     * @route("__TARGET_ENTITY__/edit/{id}",name="__TARGET_ENTITY__PreEdit")
     */
    public function edit(Entity $entity)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        //return $this->comEdit($entity);
        $action = $this->generateUrl($this->entitySetting->entityName."DoEdit",array("id"=>$entity->getId()));
        $form = $this->createMyForm($this->entitySetting->SettingPath,  $entity ,  $action );
        $show = $form->createView();
        $data = array(
            "title" => $this->entitySetting->entityCnName,
            "show"   => $show,
        );
        return $this->render("App/__TARGET_ENTITY__/edit.php.twig",$data);
    }

    /**
     * @route("__TARGET_ENTITY__/edit/{id}/do",name="__TARGET_ENTITY__DoEdit")
     *
     *
     */
    public function doEdit(Request $request,Entity $entity)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        //return $this->comDoEdit($request,$entity);
        $form = $this->createMyForm($this->entitySetting->SettingPath,  $entity );
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /**@var $entity Entity*/
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
            $urlData = [];
            return $this->js_out("保存成功",$this->generateUrl($this->entitySetting->entityName."Lists",$urlData));
        } else {
            return $this->js_out("系统错误，请联系管理员",$this->generateUrl($this->entitySetting->entityName."PreEdit",array("id"=>$entity->getId())));
        }
    }

    /**
     * @route("__TARGET_ENTITY__",name="__TARGET_ENTITY__Lists")
     */
    public function lists()
    {
        $uid = $this->needToBeAdmin();
        //私有部分
        $tools = array();
        //通用部分
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        //return $this->comLists($tools);

        /*分页配置*/
        if (!$page   = $this->getParamRequired("page")) { $page = 1;}
        $per_page   = $this->entitySetting->perPage;
        $lists = $this->getLists($page,$per_page);
        $total = $lists["total"];

        /*URL配置*/
        $urls = array(
            "toolUrl"=>$this->generateUrl($this->entitySetting->entityName."Tools"),
            "addUrl"=>$this->generateUrl($this->entitySetting->entityName."PreAdd"),
        );
        $entityCnName = $this->entitySetting->entityCnName;
        return $this->render($this->entitySetting->bundleName."/".$this->entitySetting->entityName."/list.php.twig", array(
            'title'     => "$entityCnName 列表",
            "tools"   => $tools,
            'urls'      => $urls,
            'lists'     => $lists["data"],
            'page'      => $this->creatPage($this->generateUrl($this->entitySetting->entityName."Lists"),$total,$per_page),
        ));
    }


    /**
     * @route("__TARGET_ENTITY__/tools",name="__TARGET_ENTITY__Tools")
     */
    public function tools(Request $request)
    {
        $uid = $this->needToBeAdmin();
        $this->setSetting(new __TARGET_ENTITY__Setting($this->getDoctrine()));
        return $this->comTools($request);
    }

}
